package com.tllq.zhongkang.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tllq.zhongkang.entity.*;
import com.tllq.zhongkang.service.CouponService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@Slf4j
public class CouponController {

   private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    CouponService couponService;

    /**
     * 分页查询已消费券
     * @param startime
     * @param endtime
     * @param currentpage
     * @param pagesize
     * @return
     */
    @GetMapping("/coupon")
    public PageResult findBytime(@RequestParam(value = "startime",defaultValue = "0") Long startime,
                                 @RequestParam(value = "endtime",defaultValue = "0") Long endtime,
                                 @RequestParam(value = "currentpage",defaultValue = "1") Integer currentpage,
                                 @RequestParam(value = "pagesize",defaultValue = "10") Integer pagesize){
        IPage<Couponoff> iPage = couponService.findPage(startime, endtime, currentpage, pagesize);
        return new PageResult(iPage.getCurrent(),iPage.getPages(),iPage.getTotal(),"查询成功",200,iPage.getRecords());
    }

    /**
     * 发券
     * @param coupons
     * @return
     */
    @PostMapping("/coupon/insert")
    public ResultMap insertCoupon(@RequestBody ArrayList<Coupon> coupons){
        logger.info("参数：{}",coupons);
        Message message = couponService.insertCoupon(coupons);
        if (message.getStatus()){
            ResultMap res = new ResultMap(200, message.getMsg());
            logger.info("成功：{}",res);
            return res;
        }else {
            ResultMap res = ResultMap.error(message.getMsg());
            logger.error("失败：{}",res);
            return res;
        }
    }
}
